package com.cbb.display.controller;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.annotation.Resource;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.cbb.display.entity.SysTask;
import com.cbb.display.service.SysTaskService;
import com.cbb.display.util.CommonUtil;
import com.cbb.display.enums.ErrorEnum;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bigbomb
 * @since 2020-01-18
 */
@RestController
@RequestMapping("/sys-task")
public class SysTaskController {
	@Autowired
	private SysTaskService sysTaskService;
	@Resource
	private CacheManager cacheManager;
	@GetMapping("/list")
    public JSONObject list() {
    List<SysTask> sysTaskList = sysTaskService.list();
    return CommonUtil.successJson(sysTaskList);
    	
    }
	@PostMapping("/changeStatus")
    public JSONObject changeStatus(Long jobId,String cmd) {
    int status = sysTaskService.changeStatus(jobId, cmd);
	return CommonUtil.successJson();
    	
    }
	
	@PostMapping("/addjob")
    public JSONObject addjob(String jobName,String jobNameLabel,String cronExpression) {
    LocalDateTime localDateTime= LocalDateTime.now();
    localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    Cache cache = cacheManager.getCache("cache");
    List<Map<String, Object>> jobEnumList =cache.get("jobEnumList",List.class);
    Optional<Object> jobOptional = null;
    String jobString = "";
    for(Map jobEnum:jobEnumList)
    {
    	jobOptional = Optional.ofNullable(jobEnum.get(jobName));
    	if(jobOptional.isPresent())
    	{
    		jobString = jobOptional.get().toString();
    	}
    }
    SysTask sysTask = SysTask.builder()
    		.beanClass(jobString)
    		.createBy("bigbomb")
    		.createDate(localDateTime)
    		.cronExpression(cronExpression)
    		.jobGroup("cbb")
    		.jobName(jobName)
    		.jobNameLabel(jobNameLabel)
    		.jobStatus("1")
    		.build();
    int status = sysTaskService.addJob(sysTask);
    if(status==1)
    {
    	return CommonUtil.successJson();
    }
    else {
		return CommonUtil.errorJson(ErrorEnum.E_500);
	}
    	
}
	@PostMapping("/updateCron")
    public JSONObject updateCron(Long jobid,String cronExpression) throws SchedulerException {
    int status = sysTaskService.updateCron(jobid, cronExpression);
    if(status==1)
    {
    	return CommonUtil.successJson();
    }
    else {
		return CommonUtil.errorJson(ErrorEnum.E_500);
	}
	}
	
	@PostMapping("/deljob")
    public JSONObject deljob(Long id) {
    int status = sysTaskService.remove(id);
    if(status==1)
    {
    	return CommonUtil.successJson();
    }
    else {
		return CommonUtil.errorJson(ErrorEnum.E_500);
	}
    	
    }
}
